#include "project.h"
#include "ui_project.h"
#include "blldataflow.h"
#include "globaldef.h"
#include "systempreset.h"
#include <QFileInfo>
#include <QMap>

Project *Project::self = NULL;
Project *Project::Instance()
{
    if (!self) {
        static QMutex mutex;
        QMutexLocker locker(&mutex);
        if (!self) {
            self = new Project;
        }
    }

    return self;
}

Project::Project(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Project)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();
    sqlHelper = SqlHelper::getInstance();
    appInit = AppInit::getInstance();
    appInit->start();
    bCtrl = BllController::getInstance();
    bCtrl->openSerialPort(SystemPreset::instance()->getStringData(EnumPresetItem::SerialPortName));

    //Signals and Slots
    connect(bdf, &BllDataFlow::changeMainPageIndexSignal, this, &Project::changeMainPageIndexSlot);
    connect(bdf, &BllDataFlow::returnLastMainPageSignal, this, &Project::returnLastMainPageSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &Project::retranslateUiSlot);
    connect(bCtrl, &BllController::hostVersionSignal, bdf, &BllDataFlow::hostVersionSignal);

    initWidget();
}

Project::~Project()
{
    delete ui;
}

void Project::show()
{
    QWidget::show();
    if (windowWidth >= 1280)
    {
        Project::Instance()->setGeometry((windowWidth - 1280) / 2, (windowWidth - 800) / 2, 1280, 800);
    }
}

void Project::bringToFront()
{
    hide();
    showFullScreen();
}

void Project::changeMainPageIndexSlot(int index)
{
    if (index >= 0 && index < ui->stackedWidget->count())
    {
        lastIndex = ui->stackedWidget->currentIndex();
        ui->stackedWidget->setCurrentIndex(index);
    }
}

void Project::returnLastMainPageSlot()
{
    ui->stackedWidget->setCurrentIndex(lastIndex);
}

void Project::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void Project::closeEvent(QCloseEvent *)
{
    this->deleteLater();
}

void Project::paintEvent(QPaintEvent *)
{
    static const QMap<MainPageIndex, void (Project::*)()> mapper = {
        {MainPageIndex::STARTUP, &Project::drawStartupHeader},
        {MainPageIndex::LOGIN, &Project::drawLoginHeader},
        {MainPageIndex::MAIN, &Project::drawMainUiHeader},
        {MainPageIndex::DATA, &Project::drawSubPageHeader},
        {MainPageIndex::EXPERIMENT, &Project::drawSubPageHeader},
        {MainPageIndex::CONFIG, &Project::drawSubPageHeader},
    };

    MainPageIndex pageIndex = (MainPageIndex)ui->stackedWidget->currentIndex();
    if (mapper.contains(pageIndex))
    {
        (this->*mapper[pageIndex])();
    }
}

void Project::initWidget()
{
    createDir(ReportDir);
    setStyle(StyleSheetCss);
    ui->stackedWidget->setCurrentIndex(MainPageIndex::STARTUP);
    m_headerStyleSheet = ui->header->styleSheet();
}

void Project::setStyle(QString qssFile)
{
    QFile file(qssFile);

    if (file.open(QFile::ReadOnly)) {
        QString qss = QLatin1String(file.readAll());
        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(QColor(paletteColor)));
        qApp->setStyleSheet(qss);
        file.close();
    }
}

void Project::drawStartupHeader()
{
    // 绘制背景
    QPainter painter(this);
    painter.drawPixmap(rect(), QPixmap(":/resources/background.png"), QRect());
    ui->header->setStyleSheet("");

    // 调整控件
    ui->horizontalSpacerRight->changeSize(0, 0, QSizePolicy::Fixed);
    ui->horizontalSpacerLeft->changeSize(0, 0, QSizePolicy::Expanding);
    ui->wgtUserStatus->hide();
    ui->wgtReturnMain->hide();
    ui->btnPowerOff->hide();
    ui->cTime->show();
}

void Project::drawLoginHeader()
{
    // 绘制背景
    QPainter painter(this);
    painter.drawPixmap(rect(), QPixmap(":/resources/background2.png"), QRect());
    ui->header->setStyleSheet(m_headerStyleSheet);

    // 调整控件
    ui->horizontalSpacerRight->changeSize(0, 0, QSizePolicy::Expanding);
    ui->horizontalSpacerLeft->changeSize(46, 0, QSizePolicy::Fixed);
    ui->wgtUserStatus->hide();
    ui->wgtReturnMain->hide();
    ui->btnPowerOff->show();
    ui->cTime->show();
}

void Project::drawMainUiHeader()
{
    // 绘制背景
    QPainter painter(this);
    painter.drawPixmap(rect(), QPixmap(":/resources/background2.png"), QRect());
    ui->header->setStyleSheet(m_headerStyleSheet);

    // 调整控件
    ui->horizontalSpacerRight->changeSize(0, 0, QSizePolicy::Fixed);
    ui->horizontalSpacerLeft->changeSize(0, 0, QSizePolicy::Fixed);
    ui->wgtUserStatus->show();
    ui->wgtReturnMain->hide();
    ui->btnPowerOff->show();
    ui->cTime->show();
}

void Project::drawSubPageHeader()
{
    ui->header->setStyleSheet(m_headerStyleSheet);
    ui->wgtUserStatus->show();
    ui->wgtReturnMain->show();
    ui->btnPowerOff->show();
    ui->cTime->show();  
}
